package com.txzt.gaea.business.plan.service.impl;

import com.alibaba.fastjson.JSON;
import com.txzt.gaea.business.plan.domain.model.PlanInfo;
import com.txzt.gaea.business.plan.domain.model.PlanPack;
import com.txzt.gaea.business.plan.domain.vo.PlanPackVo;
import com.txzt.gaea.business.plan.service.IPlanInfoService;
import com.txzt.gaea.business.plan.service.IPlanPackService;
import com.txzt.gaea.common.base.service.impl.BaseServiceImpl;
import com.txzt.gaea.common.core.base.context.AdminUserContext;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.common.enums.PlanStatus;
import com.txzt.gaea.provide.service.FileInfoService;
import com.txzt.gaea.provide.vo.FileInfoVo;
import com.txzt.gaea.vo.AdminUserVO;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PlanPackServiceImpl extends BaseServiceImpl<PlanPack> implements IPlanPackService {

    @Autowired
    private IPlanInfoService planInfoService;
    @Autowired
    private FileInfoService fileInfoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int savePlanPack(PlanPackVo planPackVo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        PlanPack planPack = new PlanPack();
        BeanUtils.copyProperties(planPackVo,planPack);
        planPack.setIsDelete(IsDeleteEnum.NOTDELETE.getCode());
        planPack.setCreateBy(adminUserVO.getId());
        planPack.setUpdateBy(adminUserVO.getId());
        planPack.setCreateTime(new Date());
        planPack.setUpdateTime(new Date());
        int count = sqlManager.insert(planPack,true);
        List<String> projectNoList = new ArrayList<>();
        planPackVo.getPlanInfoList().forEach(p ->{
            PlanInfo planInfo = new PlanInfo();
            planInfo.setId(p.getId());
            planInfo.setPackId(planPack.getId());
            planInfo.setPlanStatus(PlanStatus.INIT.getCode());
            planInfoService.updateById(planInfo);
            projectNoList.add(p.getProjectNo());
        });
        //文件处理
        if (null != planPackVo.getPackFile()){
            List<FileInfoVo> fileList = JSON.parseArray(planPackVo.getPackFile(), FileInfoVo.class);
            fileInfoService.fileBizTypeChange(fileList,projectNoList,1);
        }
        return count;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updatePlanPack(PlanPackVo planPackVo) {
        AdminUserVO adminUserVO = AdminUserContext.getUser();
        PlanPack planPack = new PlanPack();
        BeanUtils.copyProperties(planPackVo,planPack);
        planPack.setUpdateBy(adminUserVO.getId());
        planPack.setUpdateTime(new Date());
        int count = sqlManager.updateById(planPack);
        planPackVo.getPlanInfoList().forEach(p ->{
            PlanInfo planInfo = new PlanInfo();
            planInfo.setId(p.getId());
            planInfo.setPackId(planPack.getId());
            planInfoService.updateById(planInfo);
        });
        return count;
    }

    @Transactional()
    @Override
    public int deletePlanPack(Long id) {
        List<PlanInfo> planInfoList = planInfoService.queryByPackId(id);
        boolean flag = planInfoList.stream().anyMatch(p -> p.getPlanStatus() > PlanStatus.INIT.getCode());
        if (flag) {
            return 0;
        }else {
            List<Long> idList = planInfoList.stream().map(PlanInfo::getId).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(idList)) {
                planInfoService.removePackIdById(idList);
            }
            PlanPack old = this.selectById(id);
            if (null != old.getPackFile()){
                List<FileInfoVo> fileList = JSON.parseArray(old.getPackFile(), FileInfoVo.class);
                fileInfoService.deleteFile(fileList);
            }
//            PlanPack planPack = new PlanPack();
//            planPack.setId(id);
            old.setIsDelete(IsDeleteEnum.DELETE.getCode());
            return sqlManager.updateTemplateById(old);
        }
    }
}
